﻿/********************************************************************************************
 * 文件名称:	Movie.cs
 * 设计人员:	郑(matthewzjq@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	管理员
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;

namespace Shanfree.SharpMovie.SQLServerDAL
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class Movie : DBPersistenceBase<MovieInfo>, IMovie
    {

        #region SQL
        private const string SQL_SELECT_ALL = @"SELECT *
                                                                              FROM [dbo].[Movie]";

        private const string SQL_SELECT_IMDB = @"SELECT *
                                                                              FROM [dbo].[Movie] WHERE IMDB_ID = @IMDB_ID";

        private const string SQL_IMDB_EXIST = @"SELECT Count(1)
                                                                              FROM [dbo].[Movie] WHERE IMDB_ID = @IMDB_ID";

        private const string SQL_FRIENDS_LIST = @"SELECT 
                                                                                M.*
                                                                                FROM 
                                                                                dbo.UserRelation UR,
                                                                                dbo.Movie M,
                                                                                dbo.MyFavorite MF
                                                                                WHERE 
                                                                                MF.MovieID = M.ID
                                                                                AND MF.UserID = UR.UserBID
                                                                                AND UR.UserAID = @UserID";

        #endregion

        protected override string ConnectionString
        {
            get
            {
                return DBConnection.ConnectionString;
            }
        }

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<MovieInfo> GetList(int pageSize, int pageIndex)
        {
            string pagedSQL = CommonHelper.GetPagedSQL(SQL_SELECT_ALL, pageSize, pageIndex);

            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, pagedSQL);
            IList<MovieInfo> list = GetListFromReader(reader);
            CustomList<MovieInfo> cList = PageAmoutHelper<MovieInfo>.GetCutomList(this.ConnectionString, SQL_SELECT_ALL, pageSize, list);
            return cList;
        }

        /// <summary>
        /// 查看某个用户所有朋友的评论
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="userID">用户主键</param>
        /// <returns>评论列表</returns>
        public IList<MovieInfo> GetFriendList(string userID, int pageSize, int pageIndex)
        {
            string pagedSQL = CommonHelper.GetPagedSQL(SQL_FRIENDS_LIST, pageSize, pageIndex);
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@UserID", userID);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, pagedSQL);
            IList<MovieInfo> list = GetListFromReader(reader);
            CustomList<MovieInfo> cList = PageAmoutHelper<MovieInfo>.GetCutomList(this.ConnectionString, SQL_FRIENDS_LIST, pageSize, list);
            return cList;
        }


        /// <summary>
        ///  imdb编号对应的电影是否存在数据库中
        /// </summary>
        /// <param name="imdbNo">imdb编号</param>
        /// <returns></returns>
        public bool Exist(string imdbNo)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@IMDB_ID", imdbNo);
            object o = SqlHelper.ExecuteScalar(this.ConnectionString, CommandType.Text, SQL_IMDB_EXIST, param);
            int result = Convert.ToInt32(o);
            return (result == 0) ? false : true;
        }

        public MovieInfo GetByIMDB_NO(string imdbNo)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@IMDB_ID", imdbNo);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_IMDB, param);
            IList<MovieInfo> list = GetListFromReader(reader);
            if (list == null || list.Count == 0)
            {
                return new MovieInfo();
            }
            return list[0];
        }

    }
}
